package com.gxa.ssm340.controller;

import com.gxa.ssm340.common.PageDTO;
import com.gxa.ssm340.common.R;
import com.gxa.ssm340.entity.pojo.Reward;
import com.gxa.ssm340.entity.vo.RewardVO;
import com.gxa.ssm340.service.RewardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author 叶骏
 * @description: TODO
 * @date 2023-10-23 10:35:11
 * @version: 1.0
 */
@RestController
@Api(value = "RewardController",tags = "积分商品接口")
@RequestMapping("/reward")
public class RewardController {
    @Autowired
    private RewardService rewardService;

    @ApiOperation("积分商品列表")
    @GetMapping("/getList")
    public R getAlla( PageDTO pageDTO){
        System.out.println(pageDTO);
        final List<Reward> byPage = rewardService.getByPage(pageDTO);
        final Integer count = rewardService.count();
        final RewardVO rewardVO = new RewardVO();
        rewardVO.setCount(count);
        rewardVO.setLists(byPage);
        return R.success(rewardVO);
    }

    @ApiOperation("积分商品添加")
    @RequestMapping("/add")
    public R add(@RequestBody Reward reward){
        final Integer insert = rewardService.insert(reward);
        System.out.println(reward.getImage());
        String imgSrc = "d:/img/aa/"+reward.getImage();
        String targetPath = "d:/img";

        try {
            File imgFile = new File(imgSrc);
            File targetDir = new File(targetPath);

            if (targetDir.isDirectory()) {
                File targetFile = new File(targetDir, imgFile.getName());
                FileUtils.copyFile(imgFile, targetFile);
                System.out.println("图片复制成功！");
            } else {
                System.out.println("目标路径不是一个文件夹！");
            }
        } catch (IOException e) {
            System.out.println("发生IO异常：" + e.getMessage());
        }
        return R.success();
    }

    @ApiOperation("积分商品删除")
    @RequestMapping("/delete")
    public R delete(Integer id){
        final Integer delete = rewardService.delete(id);
        return R.success();
    }

    @PostMapping("/img")
    public R upload(@RequestParam("file") MultipartFile multipartFile) throws IOException {
//获取原始文件名: 20200724153059.png
        String originalFilename = multipartFile.getOriginalFilename();
// 从原始文件名中获取后缀,作为新添加的文件的文件名后缀(匹配)
        String subfix =originalFilename.substring(originalFilename.lastIndexOf(".")+1);
//使用UUID作为文件名,保证服务器中存放的文件名唯一
        String targetFile = UUID.randomUUID().toString().replace("-","")+"."+subfix;
// 使用绝对路径来保存上传后的文件,这里以D盘为例
        String rootPath = "d:/img/aa/";
        String fileName = targetFile;
// 构建一个空的文件对象
        File uploadFile = new File(rootPath+fileName);
        try {
//核心操作(含有异常操作, 利用异常设定状态码
// 把上传的源图像传输到目标文件中,完成上传
            multipartFile.transferTo(uploadFile);
        } catch (IOException e) {
// 抛出自定义异常, 以便被全局异常处理器拦截
// throw new BizException(ExceptionCode.WRONG_UPLOAD);
        }
// 返回服务器保存后的文件名
        return R.success(fileName);
    }

}
